package com.hangzhou.xyj.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DramaModel;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.model.type.EUserType;
import com.hangzhou.xyj.net.handler.DeleteCommentHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.DeleteCommentReq;
import com.hangzhou.xyj.ui.activity.DynamicDetailActivity;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.dialog.LoadingDialog;
import com.hangzhou.xyj.ui.listener.CheckLoginListener;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.constants.DataKey;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MyCommentAdapter extends BaseLoadingAdapter<DynamicModel> {
    private String mGroupId;//群id
    private String mEMGroupId;//环信id

    private LoadingDialog mLoadingDialog;

    public MyCommentAdapter(RecyclerView recyclerView, List<DynamicModel> data) {
        super(recyclerView, data);
        mLoadingDialog = new LoadingDialog(recyclerView.getContext());
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = super.getItemViewType(position);
        if (viewType == TYPE_LOADING_ITEM) {//加载条目
            return viewType;
        } else if (mDataList.get(position).getH5Type() == 1) {
            return DynamicModel.TYPE_IMG;
        } else {
            return mDataList.get(position).getRealShowType();
        }
    }

    @Override
    public ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case DynamicModel.TYPE_LISTEN:
                return new MyCommentViewHolderListen(mLayoutInflater.inflate(R.layout.layout_comment_listen_with_cancel_item, parent, false));
            default:
                return new MyCommentViewHolderText(mLayoutInflater.inflate(R.layout.layout_comment_text_with_cancel_item, parent, false));
        }
    }

    @Override
    public void onBindNormalViewHolder(final ViewHolder viewHolder, final int position) {
        switch (getItemViewType(position)) {
            case DynamicModel.TYPE_LISTEN:
                MyCommentViewHolderListen myViewHolder = ((MyCommentViewHolderListen) viewHolder);
                myViewHolder.setData(mDataList.get(position));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DynamicModel dynamicModel = mDataList.get(position);
                        DramaModel dramaModel = new DramaModel();
                        dramaModel.setId(dynamicModel.getId());
                        dramaModel.setName(dynamicModel.getSongName());
                        dramaModel.setNativeUrl(dynamicModel.getNativeUrl());
                        dramaModel.setAccompanyUrl(dynamicModel.getAccompanyUrl());
                        dramaModel.setLrcUrl(dynamicModel.getLrcUrl());
                        IntentManager.getInstance().goDramaPlayActivity(mContext, dramaModel);
                    }
                });
                //左滑删除按钮
                myViewHolder.mTvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteComment(mDataList.get(viewHolder.getAdapterPosition()).getMyCommentId(), viewHolder.getAdapterPosition());
                    }
                });
                break;
            default:
                ((MyCommentViewHolderText) viewHolder).setData(mDataList.get(position));
                //左滑删除按钮
                ((MyCommentViewHolderText) viewHolder).mTvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteComment(mDataList.get(viewHolder.getAdapterPosition()).getMyCommentId(), viewHolder.getAdapterPosition());
                    }
                });
                ((MyCommentViewHolderText) viewHolder).itemView.setOnClickListener(new CheckLoginListener() {
                    @Override
                    public void onClickView(View v) {
                        Intent intent;
                        DynamicModel model = mDataList.get(position);
                        if (model.getH5Type() == 1) {
                            intent = new Intent(mContext, WebActivity.class);
                            switch (model.getShowType()) {
                                case 3://热点
                                    intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_HOT_SPOT.getValue());
                                    intent.putExtra(DataKey.TITLE, "头条详情");
                                    break;
                                case 1://活动
                                    intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_ACTION.getValue());
                                    intent.putExtra(DataKey.TITLE, "活动详情");
                                    break;
                                case 2:
                                default://演出
                                    intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_SHOW.getValue());
                                    intent.putExtra(DataKey.TITLE, "演出详情");
                                    break;
                            }
                            intent.putExtra(DataKey.INDEX, model.getNativeUrl());
                            intent.putExtra(DataKey.OBJ_ID, model.getId());
                            IntentManager.getInstance().goActivity(mContext, intent);
                        } else {
                            intent = new Intent(mContext, DynamicDetailActivity.class);
                            intent.putExtra(DataKey.INDEX, model.getId());
                            intent.putExtra(DataKey.TITLE, model.getUserName());
                            IntentManager.getInstance().goActivity(mContext, intent);
                        }
                    }
                });
                break;
        }
    }

    /**
     * 删除评论
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/7 17:01
     */
    public void deleteComment(final int commentId, final int adapterPosition) {
        DeleteCommentReq commentReq = new DeleteCommentReq(commentId);
        HttpLoader.getInstance().sendRequest(new DeleteCommentHandler(commentReq, new HttpResponseListener() {
            @Override
            public void onStart() {
                mLoadingDialog.show();
            }

            @Override
            public void onSuccess(Object model) {
                mLoadingDialog.dismiss();
                //删除成功
                mDataList.remove(adapterPosition);
                notifyItemRemoved(adapterPosition);
            }

            @Override
            public void onFail(String msg) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onDone(String msg) {
                mLoadingDialog.dismiss();
            }

            @Override
            public void onError() {
                mLoadingDialog.dismiss();
            }
        }, 0));
    }

    /**
     * 音频类的ViewHolder
     */
    private class MyCommentViewHolderListen extends ViewHolder {
        CircleImageView mIvHeader;//头像
        MyTextView mTvName;//用户名
        MyTextView mTvDramaName;//戏曲名
        MyTextView mTvMyComment;//我发表的评论
        MyTextView mTvTime;//时间
        MyTextView mTvDelete;//左滑删除按钮

        public MyCommentViewHolderListen(View view) {
            super(view);
            mIvHeader = (CircleImageView) view.findViewById(R.id.civ_header);
            mTvTime = (MyTextView) view.findViewById(R.id.mtv_time);
            mTvName = (MyTextView) view.findViewById(R.id.mtv_name);
            mTvDramaName = (MyTextView) view.findViewById(R.id.mtv_drama_name);
            mTvMyComment = (MyTextView) view.findViewById(R.id.mtv_my_comment);
            mTvDelete = (MyTextView) view.findViewById(R.id.mtv_delete);
        }

        public void setData(DynamicModel data) {
            mIvHeader.displayImage(data.getUserHead());
            mTvName.setText(data.getUserName());
            mTvDramaName.setText(data.getSongName());
            mTvMyComment.setText(data.getMyComment());
            mTvTime.setText(data.getCreateTime2());
            //设置昵称右边的认证标志
            EUserType userType = PublicUtil.getUserType(data.getType(), data.getCheckFlag());
            mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, PublicUtil.getAuthenIconDrawable(mContext, userType), null);
        }
    }

    /**
     * 图文类的ViewHolder，有图也仅仅显示文字
     */
    private class MyCommentViewHolderText extends RecyclerView.ViewHolder {
        CircleImageView mIvHeader;//头像
        MyTextView mTvName;//用户名
        MyTextView mTvIntroduce;//内容
        MyTextView mTvMyComment;//我发表的评论
        MyTextView mTvTime;//时间
        MyTextView mTvDelete;//左滑删除按钮

        public MyCommentViewHolderText(View itemView) {
            super(itemView);
            mIvHeader = (CircleImageView) itemView.findViewById(R.id.civ_header);
            mTvName = (MyTextView) itemView.findViewById(R.id.mtv_name);
            mTvIntroduce = (MyTextView) itemView.findViewById(R.id.mtv_introduce);
            mTvMyComment = (MyTextView) itemView.findViewById(R.id.mtv_my_comment);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_time);
            mTvDelete = (MyTextView) itemView.findViewById(R.id.mtv_delete);
        }

        public void setData(DynamicModel data) {
            if (data.getH5Type() == 1) {
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ic_offical_head, mIvHeader);
                mTvName.setText("戏友记官方");
                mTvIntroduce.setText(data.getSongName());
                mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            } else {
                mIvHeader.displayImage(data.getUserHead());
                mTvName.setText(data.getUserName());
                //对圈子的处理
                String[] strArray = PublicUtil.getGroupInvite(data.getContent());
                if (strArray != null) {
                    String other = strArray[0];
                    mGroupId = strArray[1];
                    mTvIntroduce.setText(other + mGroupId);
                } else {
                    mTvIntroduce.setText(data.getContent());
                }
                //设置昵称右边的认证标志
                EUserType userType = PublicUtil.getUserType(data.getType(), data.getCheckFlag());
                mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, PublicUtil.getAuthenIconDrawable(mContext, userType), null);
            }
            mTvTime.setText(data.getCreateTime2());
            mTvMyComment.setText(data.getMyComment());
        }
    }

}
